import tempfile

# 1.临时文件
with tempfile.TemporaryFile() as fp:
    fp.write(b'hello world')
    fp.seek(0)
    fp.read()
# 文件调用fp.close之后, 自动关闭


# 2.临时目录
with tempfile.TemporaryDirectory() as tmpdirname:
    print('created temporary directory', tmpdirname)
# 调用 .close()时, 自动删除目录